package com.gomro.galaxy.foundation.bi.engine.listener;

import cn.gomro.cloud.core.common.utils.DateUtils;
import com.gomro.galaxy.foundation.bi.acquisition.AcquisitionService;
import com.gomro.galaxy.foundation.bi.domain.enumerater.AcquirePlan;
import com.gomro.galaxy.foundation.bi.domain.enumerater.BiAcquirePlan;
import com.gomro.galaxy.foundation.bi.domain.event.VendorStatisticsEvent;
import com.gomro.galaxy.foundation.bi.domain.event.VendorTradingVolumeEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import java.util.Date;
//供应商统计监听器
@Component
@Slf4j
public class VendorStatisticsListener implements ApplicationListener<VendorStatisticsEvent> {
    /**
     * Handle an application event.
     *
     * @param event the event to respond to
     */
    private AcquisitionService acquisitionService;

    public VendorStatisticsListener(AcquisitionService acquisitionService) {
        this.acquisitionService = acquisitionService;
    }

    @Override
    public void onApplicationEvent(VendorStatisticsEvent event) {
        Date last = (Date) event.getSource();
        Date yesterday = DateUtils.rangeDay(DateUtils.beforeNDay(new Date(), 1))[0];
        if (last.before(yesterday)) {
            log.debug("重启job。。。");
            acquisitionService.run(BiAcquirePlan.VendorStatisticsPlan.name(), "1");
        }
    }
}
